//
//  ToolsImageCache.m
//  SDWebImage
//
//  Created by 刘建镇 on 14-12-23.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ToolsImageCache.h"

@implementation ToolsImageCache

//寻找图片路径
+ (NSString *)libraryPathWithName:(NSString *)fileName
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
    path = [NSString stringWithFormat:@"%@/%@", path, fileName];
    return path;
}

//判断是否有这个文件
+ (BOOL)fileExists:(NSString *)fileName
{
    NSString *path = [ToolsImageCache libraryPathWithName:fileName];
    NSFileManager *manager = [NSFileManager defaultManager];
    return [manager fileExistsAtPath:path];
}

//取出图片的数据
+ (NSData *)dataWithFileName:(NSString *)fileName
{
    if ([ToolsImageCache fileExists:fileName]) {
        NSString *path = [ToolsImageCache libraryPathWithName:fileName];
        //取出路径
        NSData *data = [NSData dataWithContentsOfFile:path];
//        NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

        return data;
    }
    return [NSData data];
}

//存储图片数据
+ (void)saveFile:(NSData *)data fileName:(NSString *)fileName
{
    NSString *path = [ToolsImageCache libraryPathWithName:fileName];
    [data writeToFile:path atomically:YES];
}
@end
